﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;

public partial class SearchSettings : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlMuseum.DataSource = DBAccess.GetMuseumByCity("");
            ddlMuseum.DataBind();

            if (Session["MuseumId"] != null)
            {
                if (Convert.ToInt32(Session["MuseumId"]) > 0)
                {
                    ddlMuseum.SelectedValue = Session["MuseumId"].ToString();
                    ddlMuseum.Enabled = false;
                }
            }
            BindSettings();
        }
        lblResult.Visible = false;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int museumId = Convert.ToInt32(ddlMuseum.SelectedValue);
        int result = DBAccess.UpdateSearchSettings(museumId, chkArtName.Checked, chkFloor.Checked, chkStop.Checked, chkGallery.Checked, chkArtistName.Checked);
        if (result == -1)
        {
            lblResult.Text = "Search settings saved successfully.";
            lblResult.Visible = true;
        }
        else
        {
            lblResult.Text = "Error in saving Search settings.";
            lblResult.Visible = true;
        }
    }
    protected void ddlMuseum_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSettings();
    }

    private void BindSettings()
    {
        DataTable dtSearchSettings = new DataTable();
        //get the search data and bind to check boxes
        dtSearchSettings = DBAccess.GetSearchSettingsForMuseum(Convert.ToInt32(ddlMuseum.SelectedValue));
        if (dtSearchSettings != null && dtSearchSettings.Rows.Count > 0)
        {
            chkArtName.Checked = Convert.ToBoolean(dtSearchSettings.Rows[0]["IsArtName"]);
            chkArtistName.Checked = Convert.ToBoolean(dtSearchSettings.Rows[0]["IsArtistName"]);
            chkFloor.Checked = Convert.ToBoolean(dtSearchSettings.Rows[0]["IsFloor"]);
            chkStop.Checked = Convert.ToBoolean(dtSearchSettings.Rows[0]["IsStop"]);
            chkGallery.Checked = Convert.ToBoolean(dtSearchSettings.Rows[0]["IsGallery"]);
        }
        else
            ClearControls();
    }

    private void ClearControls()
    {
        chkArtName.Checked = false;
        chkArtistName.Checked = false;
        chkFloor.Checked = false;
        chkStop.Checked = false;
        chkGallery.Checked = false;
    }
}
